/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package page;

import dbase.DBconnection;
import dbase.DBanggota;
import dbase.DBkendaraan;
import dbase.DBpengguna;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.anggota;
import model.kendaraan;
import model.pengguna;
/**
 *
 * @author elfahruzzaman
 */
public class GoLogin {

    HttpServletRequest request;

    public GoLogin(HttpServletRequest request) {
        this.request = request;
    }
public String GoLogin() throws Exception {
        HttpSession session = request.getSession();
        pengguna user = new pengguna();
        anggota cek = new anggota();
        kendaraan cekKend = new kendaraan();
        DBconnection dbConn = new DBconnection();
        DBpengguna mdLogin = new DBpengguna(dbConn.getConnection());
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String hal;
        if (username.equals("") || password.equals("")) {
            return "index.jsp";
        } else {
            user = mdLogin.getUser(username);
            if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
               session.setAttribute("username", username);
               String a = user.getRole();
               int b = Integer.parseInt(a);
                if (username.equals("admin")) {
                    request.setAttribute("username", user);
                    dbConn.closeConnection();
                    return "controller?page=Lihat";
                } else {
                    request.setAttribute("username", user);
                    cek = mdLogin.cekAnggota(username);
                    cekKend = mdLogin.cekKendaraanAnggota(username);
                    if (username.equals(cek.getNrp())&& username.equals(cekKend.getNrp())){
                            hal = "daftarExisted.jsp";
                    }else if(username.equals(cek.getNrp())&& username.equals(cekKend.getNrp())){
                            hal ="registeKendaraan.jsp";
                    }else {
                            request.setAttribute("cekA", cek);
                            hal = "register.jsp";
                    }

                    dbConn.closeConnection();
                    return hal;
                    
                }
            } else {
                return "index.jsp";
            }
        }

    }}
